package view;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ButtonGroup;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;

import model.Paper;
import model.Recommendation;
import model.User;
import controller.FileWriter;
@SuppressWarnings("serial")
/**
 * This is a visual representation of a recommendation. It displays the question and a place to answer.
 * @author Janelle Tin
 *
 */
public class RecommendationPanel extends JPanel{
	private Recommendation recommendation;
	private List<String> questions;
	private ButtonGroup answer;
	private JTextArea rationale;
	
	/**
	 * The constructor for the RecommendationPanel
	 * @param theRecommendation
	 */
	public RecommendationPanel(Recommendation theRecommendation){
		super(new BorderLayout());
		recommendation = theRecommendation;
		questions = new ArrayList<String>();
		initialize();
	}
	
	/**
	 * The visuals and initializations of the Recommendation Panel including the radio buttons, the text area, and the 
	 * questions that are needed to perform the recommendation.
	 */
	private void initialize() {
		questions = recommendation.getQuestions();
		// This is hardcoded knowing the questions.
		JPanel first = new JPanel(new GridLayout(0,2,5,5)); //Q1
		// the question
		JTextArea txtQ = new JTextArea(questions.get(0));
		txtQ.setEditable(false);
		txtQ.setLineWrap(true);
		txtQ.setBackground(getBackground());
		txtQ.setWrapStyleWord(true);
		first.add(txtQ);
		//the button for Q1
		answer = new ButtonGroup();
		JPanel buttonPanel = new JPanel(new GridLayout(1,0));
		for(int i = 1; i <= 5; i++) {
			JRadioButton b = new JRadioButton(String.valueOf(i));
			b.setActionCommand(String.valueOf(i));
			answer.add(b);
			buttonPanel.add(b);
		}
		first.add(buttonPanel);
		first.setBackground(Color.BLUE);
		
		JPanel second = new JPanel(new GridLayout(0,2,5,5)); //Q2
		// the question
		JTextArea txtQ1 = new JTextArea(questions.get(1));
		txtQ1.setEditable(false);
		txtQ1.setLineWrap(true);
		txtQ1.setWrapStyleWord(true);
		txtQ1.setBackground(getBackground());
		second.add(txtQ1);
		//the answer
		rationale = new JTextArea("TEST \n\n\n\n\n\n TEST");
		second.add(rationale);
		first.setMaximumSize(new Dimension(100, 100));
		JPanel outer = new JPanel();
		outer.add(first);
		
		add(outer, BorderLayout.NORTH);
		add(second, BorderLayout.CENTER);
		//add(new JButton("Hello"), BorderLayout.SOUTH);
	}
	
	/**
	 * Returns the users answers from the radiobuttons
	 * @return answer
	 */
	public ButtonGroup getAnswer(){
		return answer;
	}
	
	/**
	 * Returns the users rationale from the textArea
	 * @return rationale
	 */
	public JTextArea getRationale(){
		return rationale;
	}
	
	/**
	 * The Work performed in order to submit a Recommendation that the SubprogramPaperPanel calls.
	 * 
	 * @param u the User
	 * @param p the Paper
	 */
	public void sumbitRec(User u, Paper p) {
		File rec = new File("recommendation.txt");
		PrintWriter writer;
		//TODO: fix case where no answer is selected.
		try {
			//System.out.println("" + recommendation.getQuestions().get(1));
			writer = new PrintWriter(rec);
			writer.println(recommendation.getQuestions().get(0) + "***" + answer.getSelection().getActionCommand() + "\n");
			writer.println(recommendation.getQuestions().get(1) + "***" + rationale.getText() + "\n");
			writer.close();
			FileWriter.writeRecommendationFile(rec, u, p);
			rec.delete();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * Getter for the recommendation.
	 * @return the recommendation
	 */
	public Recommendation getRecommendation() {
		return recommendation;
	}
}
